package com.exia.android.socialnet.twitter;


import com.exia.android.socialnet.R;

import twitter4j.TwitterException;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.webkit.WebView;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import android.view.MotionEvent;
import android.view.View;  
import android.view.View.OnClickListener;  
import android.view.View.OnTouchListener;
import android.widget.Button;


public class TwitterActivity extends Activity {
    /** Called when the activity is first created. */
	private EditText pinText;
	private EditText tweetText;
    private Button bouton;
    private Button webButton;
    private TextView textView;
    private WebView webViewer;
    private RelativeLayout webLayout;
    private Button userButton;
    private Toast toast;
    private Context context;
    private ListView listTweet;
    private String[] strListTweet;
    private ArrayAdapter<String> adapt;
    private Button msgLayoutButton;
    private RelativeLayout msgLayout;
    private EditText recipientValue;
    private EditText msgValue;
    private Button sendMsgButton;
    private Button backButton;
    private ListView listMsg;
    private ArrayAdapter<String> adaptMsg;
    private String[] strMsg;

    @Override
	public void onCreate(Bundle savedInstanceState)
    {
    	super.onCreate(savedInstanceState);
        setContentView(R.layout.twitter_tab_layout);
        
        //instanciation des composants
        bouton = (Button) findViewById(R.id.SendButton);
        pinText = (EditText) findViewById(R.id.PinValue);
        tweetText = (EditText) findViewById(R.id.tweetValue);
        textView = ((TextView)findViewById(R.id.TextViewHello));
        webViewer = ((WebView)findViewById(R.id.webView));
        webButton = ((Button)findViewById(R.id.accessButton));
        webLayout = ((RelativeLayout)findViewById(R.id.webLayout));
        msgLayout = ((RelativeLayout)findViewById(R.id.msgLayout));
        textView.setText("Bienvenue sur SocNet !");
        userButton = ((Button)findViewById(R.id.userButton));
        msgLayoutButton = ((Button)findViewById(R.id.msgLayoutButton));
        recipientValue = ((EditText)findViewById(R.id.recipientValue));
        msgValue = ((EditText)findViewById(R.id.msgText));
        sendMsgButton = ((Button)findViewById(R.id.sendMsgButton));
        backButton = ((Button)findViewById(R.id.retourButton));
        
        listTweet = ((ListView)findViewById(R.id.listTweet));
        listTweet.setTextFilterEnabled(true);
        listMsg = ((ListView)findViewById(R.id.listMsg));
        
        webLayout.setFocusableInTouchMode(true);
		webViewer.setFocusable(true);
		
		context = getApplicationContext();

        //instanciation de l'objet des préférences utilisateur
        
        SharedPreferences twitterSettings = this.getSharedPreferences("com.exia.android.socialnet", Context.MODE_PRIVATE);
       
        
        final twit tw = new twit(twitterSettings);
         
    	webViewer.getSettings().setJavaScriptEnabled(true);
    	
    	try
    	{
			webViewer.loadUrl(tw.get_url());
		} 
    	catch (TwitterException e1) 
    	{
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
    	
    	if(tw.getPreferences(twitterSettings) == false)
    	{
    		webLayout.setVisibility(0);
    		webViewer.requestFocus(View.FOCUS_DOWN);
    	}
    	else if(tw.getPreferences(twitterSettings) == true)
    	{
    		try 
    		{
    			
				tw.set_connexion("");
				strListTweet = tw.getStrTimeline();
				adapt = new ArrayAdapter<String>(context, R.layout.twitter_listview, strListTweet);
				listTweet.setAdapter(adapt);
			}
    		catch (Exception e) 
    		{
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
    	}
    	
    	
		
/////////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////////////
		
		
        //listener bouton tweet
		bouton.setOnClickListener(
			new OnClickListener()
			{
				@Override
				public void onClick(View v)
				{
					try 
					{
						toast = Toast.makeText(context, tw.set_status(tweetText.getText().toString()), Toast.LENGTH_LONG);
						toast.show();
						strListTweet = tw.getStrTimeline();
						//adapt = new ArrayAdapter<String>(context, R.layout.twitter_listview, strListTweet);
						//listTweet.setAdapter(adapt);
					} 
					catch (Exception e) 
					{
						// TODO Auto-generated catch block
						textView.setText(e.toString());
					}
				}
			}
		);
		
		
		//listener bouton web
		webButton.setOnClickListener(
			new OnClickListener()
			{
				@Override
				public void onClick(View v)
				{
					try 
					{
						webLayout.setVisibility(4);
						tw.set_connexion(pinText.getText().toString());
						strListTweet = tw.getStrTimeline();
						adapt = new ArrayAdapter<String>(context, R.layout.twitter_listview, strListTweet);
						listTweet.setAdapter(adapt);
					} 
					catch (Exception e) 
					{
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}
		);
		
		//listener bouton pour changer d'utilisateur
		userButton.setOnClickListener(
			new OnClickListener()
			{
				@Override
				public void onClick(View v)
				{
					try 
					{
						tw.setPreferences(false);
						webLayout.setVisibility(0);
						webViewer.requestFocus(View.FOCUS_DOWN);
					} 
					catch (Exception e) 
					{
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}
		);
		
		//listener pour la listview
		listTweet.setOnItemClickListener(new OnItemClickListener()
			{
			    @Override
				public void onItemClick(AdapterView<?> parent, View view, int position, long id)
			    {
			    	msgLayout.setVisibility(0);
					msgLayout.requestFocus(View.FOCUS_DOWN);
					recipientValue.setText(tw.getUserScreenname(((TextView) view).getText().toString()));
			    }
			}
		);
		
		//listener bouton accéder aux messages
		msgLayoutButton.setOnClickListener(
			new OnClickListener()
			{
				@Override
				public void onClick(View v)
				{
					try 
					{
						strMsg = tw.getMessages();
						adaptMsg = new ArrayAdapter<String>(context, R.layout.twitter_listview, strMsg);
						
						listMsg.setAdapter(adaptMsg);
						
						msgLayout.setVisibility(0);
						msgLayout.requestFocus(View.FOCUS_DOWN);
					} 
					catch (Exception e) 
					{
						((EditText)findViewById(R.id.msgText)).setText(e.toString());
						msgLayout.setVisibility(0);
						msgLayout.requestFocus(View.FOCUS_DOWN);
					}
				}
			}
		);
		
		//listener bouton envoyer un message
		sendMsgButton.setOnClickListener(
			new OnClickListener()
			{
				@Override
				public void onClick(View v)
				{
					try 
					{
						msgLayout.setVisibility(4);
						toast = Toast.makeText(context, tw.sendMsg(recipientValue.getText().toString(), msgValue.getText().toString()), Toast.LENGTH_LONG);
						toast.show();
					} 
					catch (Exception e) 
					{
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}
		);
		
		//listener bouton retour
		backButton.setOnClickListener(
			new OnClickListener()
			{
				@Override
				public void onClick(View v)
				{
					try 
					{
						msgLayout.setVisibility(4);
					} 
					catch (Exception e) 
					{
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}
		);
		
		//touch listener sur le webview
		webViewer.setOnTouchListener(new OnTouchListener() { 
		    @Override
		    public boolean onTouch(View v, MotionEvent event) {
		        switch (event.getAction()) 
		        { 
	                   case MotionEvent.ACTION_DOWN: 
	                   case MotionEvent.ACTION_UP: 
                       if (!v.hasFocus()) { 
                           v.requestFocus(); 
                       } 
                       break; 
	               } 
	               return false; 
	            }
		    }
		);
    }
}


